package chat

import (
	"bufio"
	"log"

	"github.com/libp2p/go-libp2p/core/network"
)

// 服务端处理逻辑
//
//	s	处理业务代码
func handleStream(s network.Stream) {
	log.Println("接入新的客户端：", s.ID())
	if _, ok := cache_maps[s.ID()]; !ok {
		cache_maps[s.ID()] = s
	}

	// Create a buffer stream for non-blocking read and write.
	rw := bufio.NewReadWriter(bufio.NewReader(s), bufio.NewWriter(s))

	go readData(s.ID(), rw)
	go writeData(rw)

	// stream 's' will stay open until you close it (or the other side closes it).
}
